package com.klun.likou;

public class Solution14 {

	public static void main(String[] args) {
		String[] s = {"flower", "flow", "flight"};
		String r = longestCommonPrefix(s);
		System.out.println(r);
	}

	/**
	 * 编写一个函数来查找字符串数组中的最长公共前缀。
	 *
	 * @return
	 */
	public static String longestCommonPrefix(String[] strs) {
		String result = strs[0];
		for (int i = 1; i < strs.length; i++) {
			String str = strs[i];
			int len = Math.min(result.length(), str.length());
			for (int j = 0; j < len; j++) {
				if (str.charAt(j) != result.charAt(j)) {
					if (j == 0) {
						return "";
					}
					result = str.substring(0, j);
					break;
				}
			}
			if (result.length() > len) {
				result = str.substring(0, len);
			}
		}
		return result;
	}
}